﻿<common:LayoutAwarePage
    x:Name="pageRoot"
    x:Class="Looptribe.Moodlin8.View.SearchResultsPage"
    DataContext="{Binding SearchResults, Source={StaticResource Locator}}"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Looptribe.Moodlin8.View"
    xmlns:common="using:Looptribe.Moodlin8.Common"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:uiHelper="using:Looptribe.Moodlin8.UIHelper" mc:Ignorable="d"
    d:ExtensionType="Search">

    <Page.Resources>
        <CollectionViewSource x:Name="resultsViewSource" Source="{Binding Results}" IsSourceGrouped="True"/>
        <CollectionViewSource x:Name="filtersViewSource" Source="{Binding Filters}"/>
        <common:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
        <!-- TODO: Update the following string to be the name of your app -->
        <x:String x:Key="AppName">Moodle:</x:String>

        <Style x:Key="SectionTitleStyle" TargetType="TextBlock">
            <Setter Property="FontFamily" Value="{StaticResource ContentControlThemeFontFamily}"/>
            <Setter Property="TextTrimming" Value="WordEllipsis"/>
            <Setter Property="TextWrapping" Value="NoWrap"/>
            <Setter Property="Typography.StylisticSet20" Value="True"/>
            <Setter Property="Typography.DiscretionaryLigatures" Value="True"/>
            <Setter Property="Typography.CaseSensitiveForms" Value="True"/>
            <Setter Property="FontSize" Value="26.667"/>
            <Setter Property="LineStackingStrategy" Value="BlockLineHeight"/>
            <Setter Property="FontWeight" Value="Normal"/>
            <Setter Property="LineHeight" Value="30"/>
            <Setter Property="RenderTransform">
                <Setter.Value>
                    <TranslateTransform X="-1" Y="6"/>
                </Setter.Value>
            </Setter>
            <Setter Property="Foreground" Value="{StaticResource MoodleSectionTitleColor}"></Setter>
        </Style>

        <DataTemplate x:Key="NextAssignmentsTemplate">
            <Grid Height="70" Margin="2" Width="330">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <StackPanel Orientation="Horizontal">
                    <Border Background="{StaticResource ListViewItemPlaceholderBackgroundThemeBrush}" Width="70" Height="70">
                        <ContentControl Template="{StaticResource DateBlockTemplate}" />
                        <!--<Image Source="{Binding Image}" Stretch="UniformToFill" AutomationProperties.Name="{Binding Title}"/>-->
                    </Border>
                    <Border BorderThickness="1" Width="260" Height="70" BorderBrush="#FFD3D3D3" Background="{StaticResource HubListItemBackground}">
                        <StackPanel Grid.Column="1" VerticalAlignment="Top" Margin="10,0,0,0" Width="250" >
                            <TextBlock Text="{Binding Course}" Style="{StaticResource TitleTextStyle}" TextWrapping="NoWrap"/>
                            <TextBlock Text="{Binding Title}" Style="{StaticResource SubtitleTextStyle}" TextWrapping="NoWrap"/>
                            <TextBlock Text="{Binding Subtitle}" Style="{StaticResource CaptionTextStyle}" TextWrapping="NoWrap"/>
                        </StackPanel>
                    </Border>
                </StackPanel>
            </Grid>
        </DataTemplate>
        <DataTemplate x:Key="ContactTemplate">
            <Grid Height="70" Width="330" Margin="2">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Border Background="{StaticResource ListViewItemPlaceholderBackgroundThemeBrush}" Width="70" Height="70">
                    <Image Source="{Binding Image}" Stretch="UniformToFill" AutomationProperties.Name="{Binding Title}"/>
                </Border>
                <Border Grid.Column="1" BorderThickness="1" Width="260" Height="70" BorderBrush="#FFD3D3D3" Background="{StaticResource HubListItemBackground}" HorizontalAlignment="Left">
                    <StackPanel VerticalAlignment="Bottom" Margin="10,0,0,10" Orientation="Horizontal">
                        <TextBlock Text="{Binding Title}" Style="{StaticResource TitleTextStyle}" TextWrapping="NoWrap" />
                    </StackPanel>
                </Border>
            </Grid>
        </DataTemplate>
        <DataTemplate x:Key="CoursesTemplate">
            <Grid Background="{StaticResource ListViewItemPlaceholderBackgroundThemeBrush}" Width="330" Height="70" Margin="2">
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"></RowDefinition>
                    <RowDefinition Height="10"></RowDefinition>
                </Grid.RowDefinitions>
                <TextBlock Text="{Binding Title}" Style="{StaticResource TitleTextStyle}" TextWrapping="Wrap" Foreground="White" VerticalAlignment="Bottom" Margin="10,0,5,5"/>
            </Grid>
        </DataTemplate>
        <uiHelper:SearchTemplateSelector x:Key="SearchTemplateSelector" AssignmentTemplate="{StaticResource NextAssignmentsTemplate}" ContactTemplate="{StaticResource ContactTemplate}" CourseTemplate="{StaticResource CoursesTemplate}"/>
    </Page.Resources>

    <!--
        This grid acts as a root panel for the page that defines two rows:
        * Row 0 contains the back button and page title
        * Row 1 contains the rest of the page layout
    -->
    <Grid Style="{StaticResource LayoutRootStyle}">
        <Grid.RowDefinitions>
            <RowDefinition Height="140"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <Grid x:Name="resultsPanel" Grid.Row="1">
            <!--
            The body of the page in most view states uses an items controls to create multiple radio buttons
            for filtering above a horizontal scrolling grid of search results
        -->
            <Grid x:Name="typicalPanel">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>

                <ItemsControl
                    x:Name="filtersItemsControl"
                    Canvas.ZIndex="1"
                    ItemsSource="{Binding Source={StaticResource filtersViewSource}}"
                    Visibility="{Binding ShowFilters, Converter={StaticResource BooleanToVisibilityConverter}}"
                    Margin="120,-3,120,30">

                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel Orientation="Horizontal"/>
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <RadioButton
                                GroupName="Filters"
                                IsChecked="{Binding Active, Mode=TwoWay}"
                                Checked="Filter_Checked"
                                Style="{StaticResource TextRadioButtonStyle}">
                                <TextBlock Text="{Binding Description}"  Margin="3,-7,3,10" Style="{StaticResource GroupHeaderTextStyle}" />
                            </RadioButton>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>

                <GridView
                    x:Name="resultsGridView"
                    AutomationProperties.AutomationId="ResultsGridView"
                    AutomationProperties.Name="Search Results"
                    TabIndex="1"
                    Grid.Row="1"
                    Margin="0,-238,0,0"
                    Padding="110,240,110,46"
                    SelectionMode="None"
                    IsSwipeEnabled="false"
                    IsItemClickEnabled="True"
                    ItemsSource="{Binding Source={StaticResource resultsViewSource}}"
                    ItemTemplateSelector="{StaticResource SearchTemplateSelector}">
                    <GridView.Background>
                        <ImageBrush ImageSource="ms-appx:///Assets/Background.png"/>
                    </GridView.Background>

                    <GridView.ItemContainerStyle>
                        <Style TargetType="Control">
                            <Setter Property="Margin" Value="0,0,40,0"/>
                        </Style>
                    </GridView.ItemContainerStyle>
                    <GridView.GroupStyle>
                        <GroupStyle>
                            <GroupStyle.HeaderTemplate>
                                <DataTemplate>
                                    <TextBlock Text='{Binding Key}' Foreground="{StaticResource ApplicationForegroundThemeBrush}" Margin="7,5,5,5" Style="{StaticResource SectionTitleStyle}" />
                                </DataTemplate>
                            </GroupStyle.HeaderTemplate>
                            <GroupStyle.ContainerStyle>
                                <Style TargetType="GroupItem">
                                    <Setter Property="Template">
                                        <Setter.Value>
                                            <ControlTemplate TargetType="GroupItem">
                                                <StackPanel Orientation="Vertical">
                                                    <ContentPresenter Content="{TemplateBinding Content}" />
                                                    <ItemsControl x:Name="ItemsControl" ItemsSource="{Binding GroupItems}" />
                                                </StackPanel>
                                            </ControlTemplate>
                                        </Setter.Value>
                                    </Setter>
                                </Style>
                            </GroupStyle.ContainerStyle>
                            <GroupStyle.Panel>
                                <ItemsPanelTemplate>
                                    <VariableSizedWrapGrid Orientation="Vertical" MaximumRowsOrColumns="6" />
                                </ItemsPanelTemplate>
                            </GroupStyle.Panel>
                        </GroupStyle>
                    </GridView.GroupStyle>
                    <GridView.ItemsPanel>
                        <ItemsPanelTemplate>
                            <WrapGrid Orientation="Vertical" MaximumRowsOrColumns="1" />
                        </ItemsPanelTemplate>
                    </GridView.ItemsPanel>
                </GridView>
            </Grid>

            <!--
                The body of the page when snapped uses a combo box to select a filter above a
                vertical scrolling list of search results
            -->
            <Grid x:Name="snappedPanel" Visibility="Collapsed">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>

                <ComboBox
                    ItemsSource="{Binding Source={StaticResource filtersViewSource}}"
                    Visibility="{Binding ShowFilters, Converter={StaticResource BooleanToVisibilityConverter}}"
                    Margin="20,0,20,20"
                    SelectionChanged="Filter_SelectionChanged"
                    HorizontalAlignment="Left"/>

                <ListView
                    x:Name="resultsListView"
                    AutomationProperties.AutomationId="ResultsListView"
                    AutomationProperties.Name="Search Results"
                    TabIndex="1"
                    Grid.Row="1"
                    Margin="10,0,0,0"
                    Padding="0,0,0,60"
                    SelectionMode="None"
                    IsSwipeEnabled="false"
                    IsItemClickEnabled="True"
                    ItemsSource="{Binding Source={StaticResource resultsViewSource}}"
                    ItemTemplate="{StaticResource StandardSmallIcon70ItemTemplate}">

                    <ListView.ItemContainerStyle>
                        <Style TargetType="Control">
                            <Setter Property="Height" Value="70"/>
                            <Setter Property="Margin" Value="0,0,18,8"/>
                        </Style>
                    </ListView.ItemContainerStyle>
                </ListView>
            </Grid>
        </Grid>

        <!-- Back button, page title and subtitles-->
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Button x:Name="backButton" Click="GoBack" IsEnabled="{Binding Frame.CanGoBack, ElementName=pageRoot}" Style="{StaticResource BackButtonStyle}"/>
            <TextBlock x:Name="pageTitle" Grid.Column="1" Text="{StaticResource AppName}" Style="{StaticResource MoodleAppTitleSyle}"/>
            <TextBlock x:Uid="SearchResultTitle" Grid.Column="1" HorizontalAlignment="Left" Margin="202,60,0,0" TextWrapping="Wrap" Text="Search Result" VerticalAlignment="Top" Style="{StaticResource HeaderTextStyle}" Foreground="#FF5A5A5A"/>
        </Grid>

        <TextBlock
            x:Name="noResultsTextBlock"
            Grid.Row="1"
            Margin="120,50,0,0"
            Visibility="Collapsed"
            Style="{StaticResource SubheaderTextStyle}"
            Text="No results match your search." />

        <VisualStateManager.VisualStateGroups>

            <!-- Visual states reflect the application's view state -->
            <VisualStateGroup x:Name="ApplicationViewStates">
                <VisualState x:Name="FullScreenLandscape"/>
                <VisualState x:Name="Filled"/>

                <!-- The entire page respects the narrower 100-pixel margin convention for portrait -->
                <VisualState x:Name="FullScreenPortrait">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="backButton" Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PortraitBackButtonStyle}"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="filtersItemsControl" Storyboard.TargetProperty="Margin">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="100,-3,90,30"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="resultsGridView" Storyboard.TargetProperty="Padding">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="90,240,86,56"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="noResultsTextBlock" Storyboard.TargetProperty="Margin">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="100,50,0,0"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>

                <!--
                    The back button and title have different styles when snapped in addition to more extensive changes:
                    * The query text becomes the page header
                    * A different representation for filter selection and search results is used
                -->
                <VisualState x:Name="Snapped">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="backButton" Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SnappedBackButtonStyle}"/>
                        </ObjectAnimationUsingKeyFrames>

                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="queryText" Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SnappedPageHeaderTextStyle}"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="pageTitle" Storyboard.TargetProperty="Visibility">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="resultText" Storyboard.TargetProperty="Visibility">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed"/>
                        </ObjectAnimationUsingKeyFrames>

                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="typicalPanel" Storyboard.TargetProperty="Visibility">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="snappedPanel" Storyboard.TargetProperty="Visibility">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="Visible"/>
                        </ObjectAnimationUsingKeyFrames>

                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="noResultsTextBlock" Storyboard.TargetProperty="Margin">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="20,0,0,0"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="noResultsTextBlock" Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource BaselineTextStyle}"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>

            <VisualStateGroup x:Name = "ResultStates">
                <VisualState x:Name="ResultsFound" />
                <!-- When there are no results, the results panel is replaced with an informational TextBlock -->
                <VisualState x:Name="NoResultsFound">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="resultsPanel" Storyboard.TargetProperty="Visibility">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="noResultsTextBlock" Storyboard.TargetProperty="Visibility">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="Visible"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
    </Grid>
</common:LayoutAwarePage>
